import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, Item } from 'ionic-angular';
import { ApiProvider } from '../../providers/api/api';
import { NotificationProvider } from '../../providers/notification/notification';
import { StorageProvider } from '../../providers/storage/storage';

/**
 * Generated class for the UserinfoPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-userinfo',
  templateUrl: 'userinfo.html',
})
export class UserinfoPage {
  private funid = 'sys_user_role';
  private user: any;
  private roles: string;
  private options = {
    start: 0,
    limit: 10,
    where_sql:'',
    where_type:'',
    where_value:''
  };

  constructor(
    public navCtrl: NavController, 
    public navParams: NavParams, 
    private api: ApiProvider, 
    private notification: NotificationProvider,
    private storage: StorageProvider) {
      this.user = storage.getObject("userinfo");  //获取本地缓存用户信息
  }

  ionViewDidLoad() {
    //查询用户所属角色
    this.options.where_sql = "user_id=?";
    this.options.where_type = "string";
    this.options.where_value = this.user.user_id;
    
    let res = this.api.getListPage(this.funid,this.options).subscribe(res => {
      if(res['success']){
        let items = res['data']['root'];
        if(items||items.length>0){
          //拼接字符串
          items.forEach(item => {
            this.roles += item.sys_role__role_name+';';
          });
          if(this.roles) 
            this.roles = this.roles.substring(0,this.roles.length-1);
        }
      }else{
        this.notification.showAlert(res['message']); 
      } 
      this.notification.hideLoading(); 
    })
  }

}
